﻿using NAppUpdate.Framework.Sources;
using Futajma.MigrationStudio.SharePointPersistance;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Futajma.MigrationStudio.ApplicationUpdate
{
    public class SharePoint2010UpdateSource : IUpdateSource
    {

        SharePoint2010ApplicationConfiguration.UpdateFeedItem item = new SharePoint2010ApplicationConfiguration.UpdateFeedItem() { Valid = false };

        TargetAudience targetAudience = TargetAudience.MigrationConsultant;
        string currentVersion = "";

        public SharePoint2010UpdateSource(TargetAudience targetAudience, string currentVersion)
        {
            this.targetAudience = targetAudience;
            this.currentVersion = currentVersion;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename">yxz.dll</param>
        /// <param name="basePath">itemId</param>
        /// <param name="onProgress"></param>
        /// <param name="tempLocation"></param>
        /// <returns></returns>
        public bool GetData(string filename, string basePath, Action<NAppUpdate.Framework.Common.UpdateProgressInfo> onProgress, ref string tempLocation)
        {
            var persistance = new SharePoint2010ApplicationConfiguration();

            if (item.Valid)
            {
                persistance.DownloadAttachedFileFromListItem(item.ItemId, filename, tempLocation);
            }
            else
            {
                throw new Exception("No valid update available");
            }

            return true;
        }

        public string GetUpdatesFeed()
        {
            var persistance = new SharePoint2010ApplicationConfiguration();
            item = persistance.CheckUpdateFeed(targetAudience, currentVersion);

            return string.IsNullOrEmpty(item.UpdateFeed) ? "<Feed><Tasks></Tasks></Feed>" : item.UpdateFeed;
        }
    }
}
